import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class MyGasLogic extends GetxController {
  final state = MyGasState();

  @override
  void onReady() {
    super.onReady();

    var params = Get.arguments ?? {};
    state.from = params['from'];

    getNaturalGasList();
  }

  onBack() {
    Get.back(result: {'gasCode': state.gasCode});
  }

  /// 查询绑定的燃气卡列表
  getNaturalGasList() {
    var params = {
      'page_size': '100',
      'page_num': '1',
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId')
    };
    Api.queryNaturalGas(params).listen((event) {
      print(' 绑定天然气列表  返回数据   event = $event');
      if (event['code'] == 0) {
        if (event['gas_infos'] != null) {
          state.dataArray.assignAll(List<Map<dynamic, dynamic>>.from(event['gas_infos']));
        }
      } else {
        state.dataArray = [];
        toast('${event['message']}');
      }
      Get.forceAppUpdate();
      // update();
    });
  }

  unBindNaturalGas(String gasCode) {
    var params = {
      'gas_code': gasCode,
      'access_token': SpUtil.getString('token')
    };
    Api.unBindNaturalGas(params).listen((event) {
      print(' 解绑天然气  返回数据   event = $event');
      if (event['code'] == 0) {
        toast('已经成功解除');
        getNaturalGasList();
        Get.back();
      } else {
        toast('${event['message']}');
        Get.back();
      }
    });
  }
}